Hello everyone,
choice_action = label (my_main_window, " Choice of analyse : ")
choice_action ->"left"->"form"
string actions[] = {"Analyse 1",
"Analyse 2 ",
"Analyse 3"
}
bool actionsChosen[] = {false, false, false}
DBE actionCheck = checkBox(my_main_window, "",
actions, 3)
int bitmap = get actionCheck
print bitmap
int remain
int i
for i in 0:2 do {
remain = bitmap % 2
if (remain != 0) {
actionsChosen[i] = true
if (i==0){
Analyse1=true
} else if (i==1){
Analyse2 = true
} else {
Analyse3 = true
}
} else {
actionsChosen[i] = false
}
bitmap = bitmap / 2
}
If someone was able to help I would be very grateful ! Liise - Tue Jul 19 08:14:07 EDT 2016 |
Re: Checbox Hi,
you have two options: 1. Let every checkbox only have 1 value, then you can easily check if the value is 0 or 1 for unchecked/checked. Using this method you will end up with n checkbox-DBEs.
2. Do it like you are doing right now, with several options in a single checkbox DBE. If you then want to retrieve values you have to use the bit-wise and operator "&" to check which checkbox is selected: int bitmap = get(checkBox); int firstOption = bitmap & 1 int secondOption = bitmap & 2 int thirdOption = bitmap & 4 int fourthOption = bitmap & 8 int fifthOption = bitmap & 16 int sixthOption = bitmap & 32 int seventhOption = bitmap & 64 int eighthOption = bitmap & 128 You can then check if the value is 0 for unchecked or >0 for checked.
For each new option you'd have to double the value after the "&" (so 256 is next). For more information on how this works, google "binary operator and". It boils down to the number being represented as a binary value (so the value "77" becomes "1001101"), and using the &-operator you can check if a certain bit is set or not. In the example of "77" or "1001101", the first, third, fourth and seventh bit would be set (first bit is on the right), which means that these options are checked in your DBE. |
Re: Checkboxes checked
Thank you for your help.
choice_action = label (my_main_window, " Choice : ")
choice_action ->"left"->"form"
string actions[] = {"Analyse1",
"Analyse2",
"Analyse3"
}
bool actionsChosen[] = {false, false, false}
DBE actionCheck = checkBox(my_main_window, "",
actions, 3)
int bitmap = get (actionCheck)
int firstOption = bitmap & 1
print firstOption
int secondOption = bitmap & 2
print secondOption
int thirdOption = bitmap & 4
print thirdOption
|
Re: Checkboxes checked Liise - Tue Jul 19 08:50:57 EDT 2016
Thank you for your help.
choice_action = label (my_main_window, " Choice : ")
choice_action ->"left"->"form"
string actions[] = {"Analyse1",
"Analyse2",
"Analyse3"
}
bool actionsChosen[] = {false, false, false}
DBE actionCheck = checkBox(my_main_window, "",
actions, 3)
int bitmap = get (actionCheck)
int firstOption = bitmap & 1
print firstOption
int secondOption = bitmap & 2
print secondOption
int thirdOption = bitmap & 4
print thirdOption
well the way your code is right now you print the checkbox-result right after setting them to "3" (which in bit btw is "11", therefore option 1 and 2 will be checked, if you want option 3 to be selected only you have to either set it to "4" or "0b100"). If you want to check your checkbox result later, you need to do the check in some kind of callback, for example after the user presses a button. Another way is to attach a callback to the checkbox itself, this way you have a function in your code that will get called as soon as the user clicks any of the checkboxes.
Button example:
DBE dbeCheckbox = null;
void buttonCallback(DBE button){
print "button pressed!\n";
int bitmap = get(dbeCheckbox);
int firstOption = bitmap & 1
int secondOption = bitmap & 2
int thirdOption = bitmap & 4
if(firstOption > 0){
print "option 1 is selected\n";
}
if(secondOption > 0){
print "option 2 is selected\n";
}
if(thirdOption > 0){
print "option 3 is selected\n";
}
}
DB main = centered("Test Dialog Box");
string actions[] = {"Analyse1", "Analyse2", "Analyse3"};
dbeCheckbox = checkBox(main, "", actions, 3);
button(main, "Click me", buttonCallback);
show(main);
Callback on the checkbox example:
DBE dbeCheckbox = null;
void checkboxCallback(DBE chkbox){
print "selection changed!\n";
int bitmap = get(dbeCheckbox);
int firstOption = bitmap & 1
int secondOption = bitmap & 2
int thirdOption = bitmap & 4
if(firstOption > 0){
print "option 1 is selected\n";
}
if(secondOption > 0){
print "option 2 is selected\n";
}
if(thirdOption > 0){
print "option 3 is selected\n";
}
}
DB main = centered("Test Dialog Box");
string actions[] = {"Analyse1", "Analyse2", "Analyse3"};
dbeCheckbox = checkBox(main, "", actions, 3);
set(dbeCheckbox, checkboxCallback);
show(main);
|
Re: Checkboxes checked
Thank you very much for your help it works very well now ! |
Re: Checkboxes checked Liise - Tue Jul 19 10:40:08 EDT 2016
Thank you very much for your help it works very well now ! It is possible, but you need to make sure that the global variable gets declared before the function that uses it:
void callback(){
x=10;
}
int x = 0;
callback();
won't work, as the dxl-interpreter doesn't know that there is a global variable called "x" at the point where it looks at the function.
int x = 0;
void callback(){
x=10;
}
callback();
however works, since the "x" is declared before the function that uses it. You might have to do the same thing with DBE elements if you ever plan to get() values off of them in a function. To clarify: the value does not have to be initialized, it can be undefined, but the variable must at least be declared before the function that uses it. |
Re: Checkboxes checked But when I do my set(dbeCheckbox, checkboxCallback);
even if I declared my booleans before, they don't change after the "call" of the function (but there values well change inside the function)
bool myBoolean
string actions[] = {"analyse1", "analyse2", "analyse3"};
dbeCheckbox = checkBox(my_main_window, "", actions, 3);
set(dbeCheckbox, checkboxCallback);
(in checkboxCallback I change
the value of myBoolean)
print myBoolean
and there is an error ... |
Re: Checkboxes checked Liise - Wed Jul 20 02:30:41 EDT 2016 But when I do my set(dbeCheckbox, checkboxCallback);
even if I declared my booleans before, they don't change after the "call" of the function (but there values well change inside the function)
bool myBoolean
string actions[] = {"analyse1", "analyse2", "analyse3"};
dbeCheckbox = checkBox(my_main_window, "", actions, 3);
set(dbeCheckbox, checkboxCallback);
(in checkboxCallback I change
the value of myBoolean)
print myBoolean
and there is an error ... I can't really tell where your problem is since your code is incomplete. Please post your complete code and/or the error message so I can tell you what's going wrong. If it's a lot of code or you aren't allowed to send out certain parts of it, you can remove those parts and make a smaller version that highlights your problem.
Your code above for example never calls show() or block() to display the dialog box. I also would've expected your "print myBoolean" part to be in some kind of button callback (when the user presses some kind of "start" button). Also remember that the function for the checkbox callback needs to be declared before the set() function that uses it.
Check out this example, it uses the set() callback on the checkboxes to fill the "actionsChosen" array with the values of the checkboxes and as soon as the user presses the "apply" button it will print the values accordingly.
DBE dbeCheckbox = null;
bool actionsChosen[] = {false, false, false};
void checkboxCallback(DBE chkbox){
int bitmap = get(dbeCheckbox);
int firstOption = bitmap & 1
int secondOption = bitmap & 2
int thirdOption = bitmap & 4
actionsChosen[0] = firstOption > 0;
actionsChosen[1] = secondOption > 0;
actionsChosen[2] = thirdOption > 0;
}
void buttonCallback(DB x){
if(actionsChosen[0]){
print "Start Analyse 1\n";
}
if(actionsChosen[1]){
print "Start Analyse 2\n";
}
if(actionsChosen[2]){
print "Start Analyse 3\n";
}
}
DB main = centered("Test Dialog Box");
string actions[] = {"Analyse1", "Analyse2", "Analyse3"};
dbeCheckbox = checkBox(main, "", actions, 0);
set(dbeCheckbox, checkboxCallback);
apply(main, buttonCallback);
show(main);
If your program is supposed to work differently (without a button or similar), please tell me how you expect the program to work. |
Re: Checkboxes checked
In fact I was trying to see my booleans in a bad way and not in the right place... my bad |